package jass.iss.edu.Screen;

import jass.iss.edu.Controller.*;
import jass.iss.edu.Properties.*;


import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 * Author 		: Lee Zhong De Rollei
 * Student ID	: A0079953L
 */

public class ModifyJobAdvertisementScreen extends JFrame implements ActionListener{
	
	/*Local attributes=================================================================================================*/
	//required as it references serializable objects for binary saving
	private static final long serialVersionUID = 1L;
	
	//Data Related Variables
	private String advertID;
	private JobAdvertisementDTO advert;
	private ArrayList<String> arrComp;
	private ArrayList<String> arrDept;
	private ArrayList<String> arrGeo;
	private JobAdvertisementController pController;
	

		
	//UI Related Variables
	private JPanel fieldsPanel, btnPanel;

	/*Constructors=======================================================================================================*/	
	public ModifyJobAdvertisementScreen(String pAdvertID, JobAdvertisementController pController) {		
		this.advertID = pAdvertID;
		getDataFromDAOs();
		setTitle("Modify Job Advertisement");
		setSize(600,600);	
		setResizable(false);
		initialize();
	}
	
	private void getDataFromDAOs(){
		try {
			advert = (JobAdvertisementDTO) new JobAdvertisementDAO().get(advertID);
			List<CompanyDTO> lstCompany = new CompanyDAO().getAll();
			List<DepartmentDTO> lstDept= new DepartmentDAO().getAll();
			List<GeographyDTO> lstGeo = new GeographyDAO().getAll();
							
			//Convert list of arraylist for loading to combobox
			for(int i=0; i<lstCompany.size();i++){		
				arrComp = new ArrayList<String> ();
				arrComp.add(lstCompany.get(i).getId()+": "+lstCompany.get(i).getCompanyName());				
			}
			for(int i=0; i<lstDept.size();i++){		
				arrDept = new ArrayList<String> ();
				arrDept.add(lstDept.get(i).getId()+": "+lstDept.get(i).getFunction());				
			}
			for(int i=0; i<lstGeo.size();i++){	
				arrGeo = new ArrayList<String> ();
				arrGeo.add(lstGeo.get(i).getId()+": "+lstGeo.get(i).getName());				
			}
	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	private void initialize(){		
		
		 
		//initialize field panel
		initializeFieldPanel();
		//initialize button panel
		initializeBtnPanel();
		//set frame properties
		getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		getContentPane().add(fieldsPanel);
		getContentPane().add(btnPanel);
//	
	}
	
	private void initializeFieldPanel(){
		//Create fields by reusing field panel control
		fieldsPanel  = new JPanel(new FlowLayout(FlowLayout.LEFT));
		//Create field panel
		
		JPanel pnlCompany = GetComboBoxPanel("Company","2",arrComp);
		JPanel pnlDept= GetComboBoxPanel("Department","2",arrDept);
		JPanel pnlGeo = GetComboBoxPanel("Geography","1",arrGeo);
		
		JPanel pnlPosition = GetTextFieldPanel("Position", advert.getPosition());
		JPanel pnlRef = GetLabel("Reference ID", advert.getReferenceID());
		JPanel pnlReq = GetTextArea("Requirements",advert.getRequirements());
		JPanel pnlDesc = GetTextArea("Requirements",advert.getRequirements());
		

/*
		private String position;
		private String referenceID;
		private String jobDescription;
		private String requirements;
		private Date advertisedDate;
		private Date closingDate;
		private String salaryRange;
		private String alternateDescription;
		private String jobAdvertStatus;
		private Date postedDate;
		private String responsibilities;
		
		*/
		fieldsPanel.add(pnlRef);
		fieldsPanel.add(pnlCompany);
		fieldsPanel.add(pnlDept);
		fieldsPanel.add(pnlGeo);
		fieldsPanel.add(pnlPosition);
		fieldsPanel.add(pnlReq);
		fieldsPanel.add(pnlDesc);
		
		//get fields panel properties
		fieldsPanel.setPreferredSize(new Dimension(600,200));
		fieldsPanel.setBorder(BorderFactory.createTitledBorder("Modify Advertisement"));
	}
  
	private void initializeBtnPanel(){
		 btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
		 btnPanel.setPreferredSize(new Dimension(600, 40));
		 btnPanel.add(createBtnWithListener("Save",this,"save"));
		 btnPanel.add(createBtnWithListener("Cancel",this,"cancel"));		 
	}
	
	private boolean validateSave(){
		/*
    	String complaint_text = fpComplaint.getValue();    	    
		if (complaint_text.length()<11){
    		JOptionPane.showMessageDialog(this, "Complaint should not contain less than 10 characters(including space)");
    		return false;
    	}else{
    		return true;
    	}
    	*/
    	return false;
    }
	
    //wrapper class to basically wrap lines of code
    private static JButton createBtnWithListener(String text, ActionListener list, String cmd){
	   	JButton btn = new JButton(text);
	   	btn.addActionListener(list);
	   	btn.setActionCommand(cmd);
	   	return btn;
	   }
	   	   
	/*public methods for events=========================================================================================*/
	public void actionPerformed(ActionEvent evt){	
		/*
    if(evt.getActionCommand().equals("save")){
    	if (validateSave()){
			boolean status;
			complaint.setComplaint(fpComplaint.getValue());
			complaint.setComplaintStatus(fpStatus.getValue().toUpperCase().equals("PENDING")?ComplaintStatus.Pending:ComplaintStatus.Closed);
			status = new ComplaintController().UpdateComplaint(complaint);
			//Display msg box
			if(status){
				caller.refreshGrid();
				JOptionPane.showMessageDialog(this, "Complaint Saved Successfully");
				this.dispose();
			}else{
				JOptionPane.showMessageDialog(this, "System encounter problem when saving complaint");
			}
    	}//end of if validatesave
    }else if(evt.getActionCommand().equals("cancel")){
    	this.dispose();
    }
    */
	}

	
	private JPanel GetComboBoxPanel(String fieldName, String fieldValue, ArrayList<String> pOptions) {
		 JPanel pnlField = new JPanel();
		 JPanel pnlFieldLbl = new JPanel();
		 JPanel pnlCol = new JPanel();
		 JPanel pnlFieldValue = new JPanel();
		//Display Label
		pnlFieldLbl.setLayout(new FlowLayout(FlowLayout.LEFT));
		pnlFieldLbl.setPreferredSize(new Dimension(100,20));
		pnlFieldLbl.add(new JLabel(fieldName));
		//Display ":"
		pnlCol.setLayout(new FlowLayout(FlowLayout.LEFT));
		pnlCol.setPreferredSize(new Dimension(10,20));
		pnlCol.add(new JLabel(":"));
		
		JComboBox cBox = new JComboBox();
		cBox.setPreferredSize(new Dimension(300,20));
		String option;
		for(int i=0;i<pOptions.size();i++)
		{
			option = pOptions.get(i).toString();
			cBox.addItem(option);
			if(getID(option).toUpperCase().equals(fieldValue.toUpperCase())){
				cBox.setSelectedIndex(i);
			}
			
			pnlFieldValue.add(cBox);
		}
	
		pnlField.add(pnlFieldLbl,new GridBagConstraints());
		pnlField.add(pnlCol,new GridBagConstraints());
		pnlField.add(pnlFieldValue,new GridBagConstraints());
		
		return pnlField;

	}
	
	private JPanel GetTextFieldPanel(String fieldName, String fieldValue){
		 JPanel pnlField = new JPanel();
		 JPanel pnlFieldLbl = new JPanel();
		 JPanel pnlCol = new JPanel();
		 JPanel pnlFieldValue = new JPanel();
		//Display Label
		pnlFieldLbl.setLayout(new FlowLayout(FlowLayout.LEFT));
		pnlFieldLbl.setPreferredSize(new Dimension(95,25));
		pnlFieldLbl.add(new JLabel(fieldName));
		//Display ":"
		pnlCol.setLayout(new FlowLayout(FlowLayout.LEFT));
		pnlCol.setPreferredSize(new Dimension(20,25));
		pnlCol.add(new JLabel(":"));
		//Display Text
		JTextField tf = new JTextField(fieldValue);
		pnlFieldValue.setLayout(new GridLayout(1,1));
		pnlFieldValue.setPreferredSize(new Dimension(200,25));
		pnlFieldValue.add(tf);				
		//Pack components

		pnlField.add(pnlFieldLbl,new GridBagConstraints());
		pnlField.add(pnlCol,new GridBagConstraints());
		pnlField.add(pnlFieldValue,new GridBagConstraints());
		
		return pnlField;
	}
	
	private JPanel GetTextArea(String fieldName, String fieldValue){
		 JPanel pnlField = new JPanel();
		 JPanel pnlFieldLbl = new JPanel();
		 JPanel pnlCol = new JPanel();
		 JPanel pnlFieldValue = new JPanel();
		//Instantiate Text Area & it properties
		JTextArea ta = new JTextArea(fieldValue);
		ta.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		ta.setPreferredSize(new Dimension(200,40));
		ta.setLineWrap(true);

		//Display Label
		pnlFieldLbl.setLayout(new FlowLayout(FlowLayout.LEFT));
		pnlFieldLbl.setPreferredSize(new Dimension(100,25));
		pnlFieldLbl.add(new JLabel(fieldName));
		//Display ":"
		pnlCol.setLayout(new FlowLayout(FlowLayout.LEFT));
		pnlCol.setPreferredSize(new Dimension(20,25));
		pnlCol.add(new JLabel(":"));
		//Display Text
		pnlFieldValue.setLayout(new FlowLayout(FlowLayout.CENTER));
		pnlFieldValue.setPreferredSize(new Dimension(200,40));
		pnlFieldValue.add(ta);
		
		
		pnlField.add(pnlFieldLbl,new GridBagConstraints());
		pnlField.add(pnlCol,new GridBagConstraints());
		pnlField.add(pnlFieldValue,new GridBagConstraints());
		
		return pnlField;
		
	}
	
	private JPanel GetLabel(String fieldName, String fieldValue){
		 JPanel pnlField = new JPanel();
		 JPanel pnlFieldLbl = new JPanel();
		 JPanel pnlCol = new JPanel();
		 JPanel pnlFieldValue = new JPanel();
		//Display Label
		pnlFieldLbl.setLayout(new FlowLayout(FlowLayout.LEFT));
		pnlFieldLbl.setPreferredSize(new Dimension(100,25));
		pnlFieldLbl.add(new JLabel(fieldName));
		//Display ":"
		pnlCol.setLayout(new FlowLayout(FlowLayout.LEFT));
		pnlCol.setPreferredSize(new Dimension(10,25));
		pnlCol.add(new JLabel(":"));
		//Display Text		
		pnlFieldValue.setLayout(new FlowLayout(FlowLayout.LEFT));		
		pnlFieldValue.setPreferredSize(new Dimension(200,25));
		pnlFieldValue.add(new JLabel(" "  + fieldValue));
		//Pack components
		pnlField.add(pnlFieldLbl,new GridBagConstraints());
		pnlField.add(pnlCol,new GridBagConstraints());
		pnlField.add(pnlFieldValue,new GridBagConstraints());
		
		return pnlField;
		
	}
	
	private String getID(String pVal){
		String id="";
		for(int i=0; i<pVal.length();i++){
			if(pVal.charAt(i)==':'){
				return id;
			}
			else{
				id = id+pVal.charAt(i);
			}
		}//end for
		return id;		
	}
	



}
